package com.spring.pay.alipay.controller.alipay;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.spring.pay.alipay.service.IAlipayService;
import com.spring.trade.po.TradeOrderInfo;

@RestController
@RequestMapping("/alipay")
public class AlipayApiController{
	private Logger LOGGER = Logger.getLogger(AlipayApiController.class);
	@Autowired
	private IAlipayService alipayServiceImpl;

	/**
	 * 支付宝回调
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/receiveNotify")
	public void receiveNotify(HttpServletRequest request, HttpServletResponse response) {
		LOGGER.debug(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 支付宝来回调啦...");
		String str = alipayServiceImpl.receiveNotify(request.getParameterMap());
		try {
			PrintWriter pw = response.getWriter();
			pw.println(str);
		} catch (IOException e) {
			LOGGER.error("支付宝回调返回系统异常！");
		}
	}
	
	/**
	 * 支付宝退款
	 * @param token
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("/alipayRefundRequest")
	public void getServiceList(String orderNo) {
		if (StringUtils.isBlank(orderNo)) {
			LOGGER.error("orderNo不能为空");
		}
		
		TradeOrderInfo tradeOrderInfo = new TradeOrderInfo();
		tradeOrderInfo.setOrderNo(orderNo);
		String result=alipayServiceImpl.refundRequest(tradeOrderInfo);
		LOGGER.info("支付宝退款结果："+result);
	}

	public void sign(){
			
	}
	
	/**
	 * 批量付款到支付宝账户回调
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/batchPaymentTheCallback")
	public void BatchPaymentTheCallback(HttpServletRequest request, HttpServletResponse response) {
		LOGGER.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 批量付款到支付宝账户来回调啦...");
		String str = alipayServiceImpl.batchPaymentTheCallback(request.getParameterMap());
		try {
			PrintWriter pw = response.getWriter();
			pw.println(str);
		} catch (IOException e) {
			LOGGER.error("批量付款到支付宝账户回调返回系统异常！");
		}
	}
}
